/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package df.bean.process;

import df.bean.db.conn.DBConnection;
import df.bean.db.table.Batch;

/**
 *
 * @author T.
 */
public class ProcessBatchBean {
    private Batch bt;
    private DBConnection conn;
    private String MM = "";
    private String YYYY = "";
    private String batchNo = "";
    private Boolean statusBatch = false;
    private String hosp = "";
    
    public ProcessBatchBean(String hospitalCode){
        conn = new DBConnection();
        conn.connectToLocal();
        bt = new Batch(hospitalCode, conn);
        this.setBatchNo(bt.getBatchNo());
        this.setMM(bt.getBatchNo().substring(4, 6));
        this.setYYYY(bt.getBatchNo().substring(0, 4));
        this.setHosp(hospitalCode);
    }

    private String getHosp() {
        return hosp;
    }

    private void setHosp(String hosp) {
        this.hosp = hosp;
    }
    private String getMM() {
        return MM;
    }

    private String getYYYY() {
        return YYYY;
    }

    private String getBatchNo() {
        return batchNo;
    }

    private void setMM(String MM) {
        this.MM = MM;
    }

    private void setYYYY(String YYYY) {
        this.YYYY = YYYY;
    }

    private void setBatchNo(String batchNo) {
        this.batchNo = batchNo;
    }

    private void setStatusBatch(Boolean statusBatch) {
        this.statusBatch = statusBatch;
    }

    private Boolean getStatusBatch() {
        return statusBatch;
    }
    
    private void processBatch(){
        String cmd_bank_payment_monthly = "";
        String cmd_bank_payment_monthly_402 = "";
        String cmd_payment_monthly = "";
        String cmd_stp_guarantee = "";
        String cmd_summary_daily = "";
        String cmd_summary_monthly = "";
        String cmd_summary_monthly_402 = "";
        String cmd_summary_tax_402 = "";
        String cmd_trn_daily = "";
        String cmd_trn_expense_header = "";
        conn.beginTrans();
        try{
            //cmd_bank_payment_monthly_402 ="UPDATE BANK_PAYMENT_MONTHLY_402 SET BATCH_NO='"+ this.getBatchNo() +"' WHERE HOSPITAL_CODE='"+ this.getHosp() +"' AND MM='"+this.getMM()+"' AND YYYY='"+this.getYYYY()+"'";
            //conn.executeUpdate(cmd_bank_payment_monthly_402);

            cmd_bank_payment_monthly = "UPDATE BANK_TMB_MEDIA_CLEARING SET BATCH_NO='"+ this.getBatchNo() +"' WHERE HOSPITAL_CODE='"+ this.getHosp() +"' AND MM='"+this.getMM()+"' AND YYYY='"+this.getYYYY()+"'";
            conn.executeUpdate(cmd_bank_payment_monthly);
            
            cmd_payment_monthly = "UPDATE PAYMENT_MONTHLY SET BATCH_NO='"+ this.getBatchNo() +"' WHERE  HOSPITAL_CODE='"+ this.getHosp() +"' AND MM='"+this.getMM()+"' AND YYYY='"+this.getYYYY()+"'";
            conn.executeUpdate(cmd_payment_monthly);
            
            cmd_summary_monthly = "UPDATE SUMMARY_MONTHLY SET BATCH_NO='"+ this.getBatchNo() +"' WHERE  HOSPITAL_CODE='"+ this.getHosp() +"' AND MM='"+this.getMM()+"' AND YYYY='"+this.getYYYY()+"'";
            conn.executeUpdate(cmd_summary_monthly);

            cmd_stp_guarantee = "UPDATE SUMMARY_GUARANTEE SET BATCH_NO='"+ this.getBatchNo() +"' WHERE  HOSPITAL_CODE='"+ this.getHosp() +"' AND MM='"+this.getMM()+"' AND YYYY='"+this.getYYYY()+"'";
            conn.executeUpdate(cmd_stp_guarantee);

            cmd_trn_expense_header = "UPDATE TRN_EXPENSE_HEADER SET BATCH_NO='"+ this.getBatchNo() +"' WHERE  HOSPITAL_CODE='"+ this.getHosp() +"' AND MM='"+this.getMM()+"' AND YYYY='"+this.getYYYY()+"'";
            conn.executeUpdate(cmd_trn_expense_header);
            
            cmd_trn_daily = "UPDATE TRN_DAILY SET BATCH_NO='"+ this.getBatchNo() +"' WHERE  HOSPITAL_CODE='"+ this.getHosp() +"' AND MM='"+this.getMM()+"' AND YYYY='"+this.getYYYY()+"'";
            conn.executeUpdate(cmd_trn_daily);

            //cmd_summary_daily = "UPDATE SUMMARY_DAILY SET BATCH_NO='"+ this.getBatchNo() +"' WHERE  HOSPITAL_CODE='"+ this.getHosp() +"' AND MM='"+this.getMM()+"' AND YYYY='"+this.getYYYY()+"'";
            //conn.executeUpdate(cmd_summary_daily);

            //cmd_summary_monthly_402 = "UPDATE SUMMARY_MONTHLY_402 SET BATCH_NO='"+ this.getBatchNo() +"' WHERE  HOSPITAL_CODE='"+ this.getHosp() +"' AND MM='"+this.getMM()+"' AND YYYY='"+this.getYYYY()+"'";
            //conn.executeUpdate(cmd_summary_monthly_402);

            //cmd_summary_tax_402 = "UPDATE SUMMARY_TAX_402 SET BATCH_NO='"+ this.getBatchNo() +"' WHERE  HOSPITAL_CODE='"+ this.getHosp() +"' AND MM='"+this.getMM()+"' AND YYYY='"+this.getYYYY()+"'";
            //conn.executeUpdate(cmd_summary_tax_402);
            
            if(bt.closeBATCH() && bt.createBATCH()){
                conn.commitTrans();
                this.setStatusBatch(true);
            }else{
               conn.rollBackTrans();
               this.setStatusBatch(false);
            }

        }catch(Exception err){
            conn.rollBackTrans();
            this.setStatusBatch(false);
            System.out.println(err.getMessage());
        }
    }
    public Boolean statusBatch(){
        try{
            this.processBatch();
        }catch(Exception err){
            this.setStatusBatch(false);
            System.out.println(err.getMessage());
        }
        return this.getStatusBatch();
    }
    public static void main(String arg[]){
        ProcessBatchBean bt = new ProcessBatchBean("00001");
        bt.statusBatch();
    }
}
